from django.db import models
from django.utils import timezone
from pygments.lexers import get_all_lexers
from pygments.styles import  get_all_styles
# Create your models here.

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())


class Snippet(models.Model):
    type = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=100, blank=True, default="")
    code = models.TextField(blank=True)
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default="python", max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default="friendly", max_length=100)
    owner = models.ForeignKey("auth.User", related_name="snippet")

    class Meta:
        ordering = ("created", )

    def save(self, *args, **kwargs):
        """
        在保存前可以对模型对象的参数进行预处理，eg: self.name = "aa" + 100
        """
        super(self.__class__, self).save(*args, **kwargs)


class Ads(models.Model):
    owner = models.ForeignKey("auth.User")
    cms = models.TextField(default="")
    title = models.CharField(max_length=255, default="")
    category = models.CharField(max_length=255, default="")
    views = models.IntegerField(default=0)
    hits = models.IntegerField(default=0)
    create_time = models.DateTimeField(default=timezone.now)
    modified_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.cms


class AdsLayer(models.Model):
    name = models.CharField(max_length=255, default="", unique=True)  # 广告类型
    description = models.CharField(max_length=255, default="")  # 广告类型描述
    create_time = models.DateTimeField(default=timezone.now)
    modified_time = models.DateTimeField(default=timezone.now)
    owner = models.IntegerField(default=1)
    creator = models.IntegerField(default=1, null=True)

    def __str__(self):
        return self.name


class AdsAuditList(models.Model):
    """
    Ads audit list
    """
    STATUS_CHOICES = [
        (0, "审核未通过"),
        (1, "审核中"),
        (2, "审核通过已上架"),
        (3, "审核通过以下架")
    ]

    ad = models.ForeignKey(Ads)
    ad_layer = models.ForeignKey(AdsLayer)
    status = models.IntegerField(choices=STATUS_CHOICES, default=1)
    auditor = models.IntegerField(default=1)
    comment = models.CharField(max_length=100, default="")
    create_time = models.DateTimeField(default=timezone.now)
    modified_time = models.DateTimeField(default=timezone.now)
    owner = models.IntegerField(default=1)

    def __str__(self):
        return "{}{} 审核状态是 {}".format(self.ad_layer, self.ad, self.STATUS_CHOICES[self.status])

    class Meta:
        default_related_name = "ads_audit_list"


